<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/10
 * Time: 13:16
 */

namespace app\admin\model;


class CategoryList extends DataBase
{
    protected $name = 'category_list';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    public function categoryIndexData()
    {
        $page_config = [
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        ];

        $map = [];
        $key = input('key'); $value = input('value');
        if(!empty($key) && !empty($value)) $map[] = [$key,'=',$value];

        $order = ['status'=>'DESC','sort'=>'DESC'];
        return  $this->where($map)->order($order)->paginate($page_config);

    }

    public function categoryChangeStatus()
    {

        $id = input('get.id');
        if(empty($id)) return ['code'=>-100,'msg'=>'传参错误'];

        $status = $this->where('id','=',$id)->value('status');
        $status = $status == 1 ? 0 : 1;
        $res = $this->save(['status'  => $status],['id' => $id]);

        if(!empty($res)) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }

    public function categoryAdd()
    {


        if(!empty($_FILES['pic']['tmp_name'])) $newData['pic'] = oosUpload($_FILES['pic'],'qqcategory');
        if(!empty($_FILES['pic_pitch']['tmp_name']))  $newData['pic_pitch'] = oosUpload($_FILES['pic_pitch'],'qqcategory');
        $newData['name'] = input('name','');
        $newData['status'] = input('status','');
        $newData['type'] = input('type');
        $newData['sort'] = input('sort');
        $newData['keyword'] = input('keyword');
        //return ['code'=>100,'msg'=>$newData];
        $re = $this->where('name','=',input('name'))->find();
        if(!empty($re)) return ['code'=>-100,'msg'=>'该类别已存在'];
        $res = $this->save($newData);
        if(!empty($res)) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];
    }

}